// SimNet is a Second Life(tm) sim mangement tool that was designed for Fermi Sandbox
// Copyright (C) 2011 Arthur Fermi
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To receive a copy of this license please visit http://www.gnu.org/licenses/

// SimNet - Land

// Enter the URL to your SimNet system, make sure to include the last /
// This will likely be http://<my-domain.com>/simnet/
string  gstrURL = "http://mywebsite.com/simnet/";

// Do not change anything below here or you will likely break something
string  gstrHTTP;
integer gintComm1;
integer gintListen1;

default
{
    state_entry()
    {
        // Get URL for web site
        llMessageLinked(LINK_SET, 333, "Getting URL", NULL_KEY);
        
       // Set Manger Name        
        list lstParcelDetails = [PARCEL_DETAILS_NAME];
        list lstParcelName=llGetParcelDetails(llGetPos(),lstParcelDetails);
        llSetObjectName("SimNet Land: " + (string)lstParcelName);

        // Get Comm Channels
        gstrHTTP = llHTTPRequest(gstrURL + "simnet_toolbar.cn",[HTTP_METHOD, "POST"], "");   
                                                       
        
        // Set timer for 24 hours to check for channel updates
        llSetTimerEvent(86400);
        
    }
    
    listen(integer channel, string name, key id, string message)
    {
        if (channel == gintComm1)
        {
            // Break out function
            list lstMessage2List;   // Temp Message List
            string strFunction;     // Function Requested
            string strInfo;         // Information for function
            string strInfo2;        // Second Level of information, mostly for banning
            
            // Convert Message to a list
            lstMessage2List = llParseString2List(message,["^"],[]);                        
            
            // Set Function
            strFunction = llList2String(lstMessage2List, 0);
            
            // Set Info
            strInfo = (string)llList2String(lstMessage2List, 1);
            
            // Set Info2
            strInfo2 = (string)llList2String(lstMessage2List, 2);
            
            
            if (strFunction == "Music")
            {
                // Set Music URL
                llSetParcelMusicURL(strInfo);
            }
            else if (strFunction == "Video")
            {
                // Set Video URL
                llParcelMediaCommandList( [PARCEL_MEDIA_COMMAND_URL, strInfo] );
            }
            else if (strFunction == "Home")
            {
                // Send AV Home
                llTeleportAgentHome(strInfo);
            }
            else if (strFunction == "Banned")
            {
                // Ban AV
                llTeleportAgentHome(strInfo);
                llAddToLandBanList(strInfo, (integer)strInfo2);
            }
            else if (strFunction == "Unbanned")
            {
                // Unban AV
                llRemoveFromLandBanList(strInfo);
            }                
            else if (strFunction == "Ejected")
            {
                // Eject AV
                llEjectFromLand(strInfo);
            }                
        }
    }
    
    timer()
    {
        // Get Comm Channels
        gstrHTTP = llHTTPRequest(gstrURL,[HTTP_METHOD, "POST"], "");   
    }
   
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == gstrHTTP)
        {
            
            // Set Comm Channel
            gintComm1 =  ((integer)llStringTrim(body, STRING_TRIM) * 7) / 3;            
            
            // clear out old lisens
            llListenRemove(gintListen1);
            
            // Set listens
            gintListen1 = llListen(gintComm1, "", "", "");
        }
    }    
    
    on_rez(integer start_param)
    {
        llResetScript();
    }
}
